/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
// FIXED: chrome 40 以下不支持 closet
Element.prototype.matches =
    Element.prototype.matches ||
    (Element.prototype as any).msMatchesSelector ||
    Element.prototype.webkitMatchesSelector;

Element.prototype.closest =
    Element.prototype.closest ||
    function closest(this: Element, selector: string) {
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let el = this;
        do {
            if (Element.prototype.matches.call(el, selector)) {
                return el;
            }
            el = (el.parentElement || el.parentNode) as Element;
        } while (el !== null && el.nodeType === 1);
        return null;
    };

// POLYFILLS
// Event.composedPath
// eslint-disable-next-line func-names
(function (E: any, d: Document, w: Window) {
    if (!E.composedPath) {
        // eslint-disable-next-line func-names
        E.composedPath = function (): EventTarget[] {
            if (this.path) {
                // eslint-disable-next-line @typescript-eslint/no-unsafe-return
                return this.path;
            }
            let target: EventTarget | null = this.target;
            this.path = [];
            while ((target as Node)?.parentNode !== null) {
                this.path.push(target);
                target = (target as Node).parentNode;
            }
            this.path.push(d, w);
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return
            return this.path;
        };
    }
})(Event.prototype, document, window);
